<template>
  <div>
    <el-table style="width: 100%" border :data="records">
      <el-table-column width="80" type="index" label="序号" align="center">
      </el-table-column>
      <el-table-column width="width" prop="skuName" label="名称">
      </el-table-column>
      <el-table-column width="width" prop="skuDesc" label="描述">
      </el-table-column>
      <el-table-column width="110" prop="prop" label="默认图片">
        <template slot-scope="{ row, $index }">
          <img :src="row.skuDefaultImg" style="width: 80px; height: 80px" />
        </template>
      </el-table-column>
      <el-table-column width="80" prop="weight" label="重量"> </el-table-column>
      <el-table-column width="80" prop="price" label="价格"> </el-table-column>
      <el-table-column width="width" prop="prop" label="操作">
        <template slot-scope="{ row, $index }">
          <HintButton
            title="上架"
            type="success"
            icon="el-icon-top"
            size="mini"
            v-if="!row.isSale"
            @click="sale(row)"
          ></HintButton>
          <HintButton
            type="success"
            icon="el-icon-bottom"
            size="mini"
            v-else
            @click="cancel(row)"
            title="下架"
          ></HintButton>
          <HintButton
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit"
            title="修改"
          ></HintButton>
          <HintButton
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="showSkuInfo(row)"
            title="展示详情"
          ></HintButton>
          <HintButton
            type="danger"
            icon="el-icon-delete"
            size="mini"
            title="删除"
          ></HintButton>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="getSkuList"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      :total="total"
      style="text-align: center"
      layout="prev, pager, next,jumper,->,sizes,total"
    >
    </el-pagination>

    <!-- 抽屉 -->
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5"> 名称</el-col>
        <el-col :span="16"> {{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 描述</el-col>
        <el-col :span="16"> {{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 价格</el-col>
        <el-col :span="16"> {{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 平台属性</el-col>
        <el-col :span="16">
          <el-tag
            type="success"
            v-for="(attr, index) in skuInfo.skuAttrValueList"
            :key="attr.id"
            style="margin-right: 10px"
            >{{ attr.attrId }}-{{ attr.valueId }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="300px" style="width: 400px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" style="width: 100%; height: 100%" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1, // 当前在第几页
      limit: 10, // 每页有几条数据
      total: 0, // 一共几条数据
      records: [], // sku列表数据
      skuInfo: {}, // sku详情
      drawer: false, // 控制抽屉显示与隐藏
    };
  },
  methods: {
    // 获取sku列表
    async getSkuList(page = 1) {
      this.page = page;
      let result = await this.$API.sku.reqSkuList(this.page, this.limit);
      if (result.code === 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    // 改变分页器每页展示条数
    handleSizeChange(pageSize) {
      this.limit = pageSize;
      this.getSkuList();
    },
    // 上架sku
    async sale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code === 200) {
        row.isSale = 1;
        this.$message.success("上架成功");
      }
    },
    // 下架sku
    async cancel(row) {
      let result = await this.$API.sku.reqCancel(row.id);
      if (result.code === 200) {
        row.isSale = 0;
        this.$message.success("下架成功");
      }
    },
    // 更新sku
    edit() {},
    // 展示sku详情
    async showSkuInfo(row) {
      this.drawer = true;
      let result = await this.$API.sku.reqSkuById(row.id);
      if (result.code === 200) {
        this.skuInfo = result.data;
      }
    },
  },
  mounted() {
    this.getSkuList();
  },
};
</script>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px;
}
>>> .el-carousel__button {
  height: 10px;
  width: 10px;
  border-radius: 5px;
  background-color: #f47983;
}
</style>
